
function widget_selectInit(domNode) {
	
	jQuery(domNode).find('.widget-select').each(function() {
		var select = jQuery(this);
		
		var metadata = window.babAddonWidgets.getMetadata(select.attr('id'));
		if (metadata.optgroupselect)
		{
			var optgroupselect = jQuery('#'+metadata.optgroupselect);
			
			// dump optgroups in memory
			window.babAddonWidgetSelect = new Object();
			 
			
			
			select.find('option').each(function() {
				var option = jQuery(this);
				var optgroup = option.parent('optgroup').attr('label');
				
				if (!optgroup)
				{
					return;
				}
				
				if (!window.babAddonWidgetSelect[optgroup])
				{
					window.babAddonWidgetSelect[optgroup] = new Array();
				}
				
				if ('' != option.attr('value'))
				{
					window.babAddonWidgetSelect[optgroup].push([option.attr('value'), option.text()]);
				}
			});
			

			
			optgroupselect.change(function() {
				var optgroup = jQuery(this).find('option:selected').text();
				
				select.empty();
				select.append('<option value=""></option>');
				
				if (!window.babAddonWidgetSelect[optgroup])
				{
					// build all optgroups
					for (var optgroup in window.babAddonWidgetSelect)
					{
						var oo = select.append('<optgroup label="'+optgroup+'"></optgroup>');
						for(var i =0; i < window.babAddonWidgetSelect[optgroup].length; i++)
						{
							var pair = window.babAddonWidgetSelect[optgroup][i];
							oo.append('<option value="'+pair[0]+'">'+pair[1]+'</option>');
						}
					}
					
					return 0;
				}
				
				
				
				
				for(var i =0; i < window.babAddonWidgetSelect[optgroup].length; i++)
				{
					var pair = window.babAddonWidgetSelect[optgroup][i];
					select.append('<option value="'+pair[0]+'">'+pair[1]+'</option>');
				}
				
			});
			
			
		}
		
		
	});

}


window.bab.addInitFunction(widget_selectInit);


